package com.intelligent.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.intelligent.mapper.ProcessManagementMapper;
import com.intelligent.pojo.ProcessManagement;
import com.intelligent.service.ProcessManagementService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
* @author lenovo
* @description 针对表【process_management(工艺管理表)】的数据库操作Service实现
* @createDate 2025-09-01 17:28:54
*/
@Service
public class ProcessManagementServiceImpl extends ServiceImpl<ProcessManagementMapper, ProcessManagement>
    implements ProcessManagementService{

    @Override
    public IPage<ProcessManagement> getProcessManagementPage(Page<ProcessManagement> page,
                                                           String processCode,
                                                           String processName,
                                                           String productCode,
                                                           String productName,
                                                           Integer status) {
        return baseMapper.selectProcessManagementPage(page, processCode, processName, productCode, productName, status);
    }

    @Override
    public boolean addProcessManagement(ProcessManagement processManagement) {
        // 自动生成工艺编号
        String processCode = generateProcessCode();
        processManagement.setProcessCode(processCode);
        
        // 设置默认状态为启用
        if (processManagement.getStatus() == null) {
            processManagement.setStatus(0);
        }
        
        // 插入数据
        int result = baseMapper.insertProcessManagement(processManagement);
        return result > 0;
    }

    @Override
    public boolean updateProcessManagement(ProcessManagement processManagement) {
        // 检查记录是否存在
        ProcessManagement existingProcess = this.getById(processManagement.getId());
        if (existingProcess == null) {
            throw new RuntimeException("工艺管理记录不存在");
        }
        
        // 更新数据
        int result = baseMapper.updateProcessManagement(processManagement);
        return result > 0;
    }

    @Override
    public boolean deleteProcessManagement(Long id) {
        // 检查记录是否存在
        ProcessManagement existingProcess = this.getById(id);
        if (existingProcess == null) {
            throw new RuntimeException("工艺管理记录不存在");
        }
        
        // 删除数据
        int result = baseMapper.deleteProcessManagement(id);
        return result > 0;
    }

    /**
     * 自动生成工艺编号
     * 格式：GC + 年月日 + 4位序号，例如：GC202501010001
     * @return 生成的工艺编号
     */
    private String generateProcessCode() {
        // 获取当前日期
        String currentDate = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        
        // 查询当天最大编号
        String maxCode = baseMapper.getMaxProcessCode();
        
        int sequence = 1;
        if (maxCode != null && maxCode.startsWith("GC" + currentDate)) {
            // 提取序号部分
            String sequenceStr = maxCode.substring(10); // GC20250101 + 4位序号
            try {
                sequence = Integer.parseInt(sequenceStr) + 1;
            } catch (NumberFormatException e) {
                sequence = 1;
            }
        }
        
        // 格式化序号为4位数字
        String formattedSequence = String.format("%04d", sequence);
        
        return "GC" + currentDate + formattedSequence;
    }

    @Override
    public ProcessManagement getProcessManagementById(Long id) {
        if (id == null) {
            throw new RuntimeException("记录ID不能为空");
        }
        
        ProcessManagement processManagement = baseMapper.selectProcessManagementById(id);
        if (processManagement == null) {
            throw new RuntimeException("工艺管理记录不存在");
        }
        
        return processManagement;
    }
}




